<template>
    <div>
        <!--面包屑导航区-->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统管理</el-breadcrumb-item>
            <el-breadcrumb-item>角色列表</el-breadcrumb-item>
        </el-breadcrumb>

        <!--卡片视图区域-->
        <el-card>
            <el-row>
                <el-button type="primary">添加角色</el-button>
            </el-row>
            <!--表格区域-->
            <el-table
                    :data="tableData"
                    width="100%"
                    border
                    stripe>
                <el-table-column type="expand" width="50">
                    <template slot-scope="scope">
                        <!--:class 动态加入我们的class 为第一个加上top-->
                        <el-row :class="['bdbottom', idx === 0 ? 'bdtop':'','vcenter']"
                                v-for="(item, idx) in scope.row.children"
                                :key="item.id">
                            <!--渲染一级权限-->
                            <el-col :span="5">
                                <el-tag type="primary" closable @close="removePermById(scope.row,item.id)">
                                    {{item.name}}
                                </el-tag>
                                <i class="el-icon-caret-right"></i>
                            </el-col>
                            <!--渲染二三级-->
                            <el-col :span="19">
                                <!-- 不是第一行就不加-->
                                <el-row :class="[idx1 !== 0 ? 'bdtop':'','vcenter']"
                                        v-for="(item1,idx1) in item.children" :key="item1.id">
                                    <el-col :span="6">
                                        <el-tag type="success" closable @close="removePermById(scope.row,item1.id)">
                                            {{item1.name}}
                                        </el-tag>
                                        <i class="el-icon-caret-right"></i>
                                    </el-col>
                                    <el-col :span="18">
                                        <el-tag v-for="(item2,idx2) in item1.children" :key="item2.id"
                                                @close="removePermById(scope.row,item2.id)" type="warning" closable>
                                            {{item2.name}}
                                        </el-tag>
                                    </el-col>
                                </el-row>
                            </el-col>
                        </el-row>
                    </template>
                </el-table-column>
                <el-table-column type="index" width="50" label="#"></el-table-column>
                <el-table-column prop="name" label="角色名称"></el-table-column>
                <el-table-column prop="descpt" label="角色描述"></el-table-column>
                <el-table-column label="角色操作" width="300px">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" icon="el-icon-edit">编辑</el-button>
                        <el-button type="danger" size="mini" icon="el-icon-delete">删除</el-button>
                        <el-button type="warning" size="mini" icon="el-icon-setting" @click="showSetPerm(scope.row.id)">分配权限
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>

        <!-- 控制权限分配对话框  -->
        <el-dialog
                title="分配权限"
                :visible.sync="setPermDialogVisible"
                width="50%"
                @close="closeEditPerm">
            <!-- 主体区域-->
            <el-tree show-checkbox node-key="id" default-expand-all ref="treeRef"
                     :default-checked-keys="defKeys"
                     :data="permList" :props="defaultProps" @node-click="handleNodeClick">
            </el-tree>
            <!--按钮区域-->
            <span slot="footer" class="dialog-footer">
                <el-button @click="setPermDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="setPermData">确 定</el-button>
              </span>
        </el-dialog>

    </div>
</template>

<script>
    export default {
        name: "Role",
        data() {
            return {
                tableData: [
                    {
                        id: 1,
                        name: '人民教师',
                        descpt: '我是个讲师',
                        children: [
                            {
                                id: 1,
                                order: 1,
                                name: '系统管理',
                                path: 'sysManage',
                                children: [
                                    {
                                        id: 11,
                                        order: 11,
                                        name: '用户列表',
                                        path: 'userList',
                                        children: [
                                            {
                                                id: 111,
                                                order: 0,
                                                name: '用户查询',
                                                path: 'getUser',
                                            },
                                            {
                                                id: 112,
                                                order: 1,
                                                name: '用户编辑',
                                                path: 'editUser',
                                            }, {
                                                id: 113,
                                                order: 1,
                                                name: '用户删除',
                                                path: 'deleteUser',
                                            }
                                        ]
                                    }, {
                                        id: 12,
                                        order: 12,
                                        name: '角色列表',
                                        path: 'roleList',
                                        children: [
                                            {
                                                id: 121,
                                                order: 0,
                                                name: '角色查询',
                                                path: 'getRole',
                                            },
                                            {
                                                id: 122,
                                                order: 1,
                                                name: '角色编辑',
                                                path: 'editRole',
                                            }, {
                                                id: 123,
                                                order: 1,
                                                name: '角色删除',
                                                path: 'deleteRole',
                                            }
                                        ]
                                    }, {
                                        id: 13,
                                        order: 13,
                                        name: '权限列表',
                                        path: 'permList',
                                        children: [
                                            {
                                                id: 131,
                                                order: 0,
                                                name: '权限查询',
                                                path: 'getPerm',
                                            },
                                            {
                                                id: 132,
                                                order: 1,
                                                name: '权限编辑',
                                                path: 'editPerm',
                                            }, {
                                                id: 133,
                                                order: 1,
                                                name: '权限删除',
                                                path: 'deletePerm',
                                            }
                                        ]
                                    }
                                ]
                            }, {
                                id: 2,
                                order: 2,
                                name: '其他管理',
                                path: 'otherManage',
                                children: [
                                    {
                                        id: 21,
                                        order: 21,
                                        name: '其他列表1',
                                        path: 'other1List',
                                    }, {
                                        id: 22,
                                        order: 22,
                                        name: '其他列表2',
                                        path: 'other2List',
                                    }, {
                                        id: 23,
                                        order: 23,
                                        name: '其他列表3',
                                        path: 'other3List',
                                    }
                                ]
                            }
                        ]
                    }, {
                        id: 2,
                        name: '放牛娃',
                        descpt: '放牛的娃娃',
                        children: [
                            {
                                id: 1,
                                order: 1,
                                name: '系统管理',
                                path: 'sysManage',
                                children: [
                                    {
                                        id: 11,
                                        order: 11,
                                        name: '用户列表',
                                        path: 'userList',
                                    }, {
                                        id: 12,
                                        order: 12,
                                        name: '角色列表',
                                        path: 'roleList',
                                    }, {
                                        id: 13,
                                        order: 13,
                                        name: '权限列表',
                                        path: 'permList',
                                    }
                                ]
                            }
                        ]
                    }
                ],
                setPermDialogVisible: false,
                permList: [
                    {
                        id: 1,
                        order: 1,
                        name: '系统管理',
                        path: 'sysManage',
                        children: [
                            {
                                id: 11,
                                order: 11,
                                name: '用户列表',
                                path: 'userList',
                                children: [
                                    {
                                        id: 111,
                                        order: 0,
                                        name: '用户查询',
                                        path: 'getUser',
                                    },
                                    {
                                        id: 112,
                                        order: 1,
                                        name: '用户编辑',
                                        path: 'editUser',
                                    }, {
                                        id: 113,
                                        order: 1,
                                        name: '用户删除',
                                        path: 'deleteUser',
                                    }
                                ]
                            }, {
                                id: 12,
                                order: 12,
                                name: '角色列表',
                                path: 'roleList',
                                children: [
                                    {
                                        id: 121,
                                        order: 0,
                                        name: '角色查询',
                                        path: 'getRole',
                                    },
                                    {
                                        id: 122,
                                        order: 1,
                                        name: '角色编辑',
                                        path: 'editRole',
                                    }, {
                                        id: 123,
                                        order: 1,
                                        name: '角色删除',
                                        path: 'deleteRole',
                                    }
                                ]
                            }, {
                                id: 13,
                                order: 13,
                                name: '权限列表',
                                path: 'permList',
                                children: [
                                    {
                                        id: 131,
                                        order: 0,
                                        name: '权限查询',
                                        path: 'getPerm',
                                    },
                                    {
                                        id: 132,
                                        order: 1,
                                        name: '权限编辑',
                                        path: 'editPerm',
                                    }, {
                                        id: 133,
                                        order: 1,
                                        name: '权限删除',
                                        path: 'deletePerm',
                                    }
                                ]
                            }
                        ]
                    }, {
                        id: 2,
                        order: 2,
                        name: '其他管理',
                        path: 'otherManage',
                        children: [
                            {
                                id: 21,
                                order: 21,
                                name: '其他列表1',
                                path: 'other1List',
                            }, {
                                id: 22,
                                order: 22,
                                name: '其他列表2',
                                path: 'other2List',
                            }, {
                                id: 23,
                                order: 23,
                                name: '其他列表3',
                                path: 'other3List',
                            }
                        ]
                    }
                ],
                // 树形控件属性绑定
                defaultProps: {
                    /*父子嵌套关联字段*/
                    children: 'children',
                    /*看到哪個屬性*/
                    label: 'name'
                },
                /*默认选中节点id值, 默認最後一級節點即可*/
                defKeys: [
                    111, 112
                ],
                /*当前新选中的角色ID值*/
                roleId: ''
            }
        },
        created() {
            this.getRoleList()
        }
        ,
        methods: {
            async getRoleList() {
                /*const {data: res} = await this.$http.post('getRoleList');
                if (res.code + '' === '200') {
                    this.tableData = res.data;
                }*/
            },
            // 刪除标签(第一个是当前角色数据, 第二个是当前标签ID)
            async removePermById(row, id) {
                const confirm = await this.$confirm('此操作将删除该权限数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).catch(err => err);
                console.log(confirm); // confirm || cancel
                if (confirm === 'confirm') {
                    this.$message.success("已确认删除");
                    this.getRoleList();

                    /*const {data: res} = await this.$http.post('users', {id: id});
                    if (res.code + '' === '200') {
                        this.$message.success("已确认删除");
                        row.children = res.data.children;
                    } else {
                        return this.$message.error("删除失败, 请稍后重试");
                    }*/
                } else {
                    return this.$message.info("已取消删除");
                }
            },
            showSetPerm(roleId) {
                // TODO 獲取全部權限數據
                this.roleId = roleId;
                this.setPermDialogVisible = true;
            },
            // 節點點擊事件
            handleNodeClick() {

            },
            // 获取所有三级节点的数据
            getLeafKeys(node, arr) {
                if (!node.children) {
                    return arr.push(node.id);
                }
                node.children.forEach(item => this.getLeafKeys(item, arr))
            },
            // 关闭对话框清空数据
            closeEditPerm() {
                this.defKeys = [];
            },
            // 分配权限
            setPermData() {
                // 获取全选和半选的iD
                const checkNode = [
                    ...this.$refs.treeRef.getCheckedKeys(),
                    ...this.$refs.treeRef.getHalfCheckedKeys()
                ];
                const keys = checkNode.join(',');
                console.log(keys);
                console.log(this.roleId);
                // TODO 请求更新我们的权限列表 keys, this.roleId
                // 刷新列表并且关闭对话框
                this.getRoleList();
                this.setPermDialogVisible = false;
            }

        }
    }
</script>

<style lang="less" scoped>
    .bdtop {
        border-top: 1px solid #EEE;
    }

    .bdbottom {
        border-bottom: 1px solid #EEE;
    }

    .el-tag {
        margin: 7px;
    }

    .vcenter {
        display: flex;
        align-items: center;
    }
</style>
